﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Text;
using System.Data;
using Maticsoft.Common;

namespace Admin.Finance.SubInvoicePayMent
{
    public partial class Add : System.Web.UI.Page
    {
        ChinaSoft.BLL.BLL_tb_MidSoft_FinanceSubInvoicePayment bll = new ChinaSoft.BLL.BLL_tb_MidSoft_FinanceSubInvoicePayment();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Params["SubInvoiceId"] != null && Request.Params["SubInvoiceId"].Trim() != "")
                {
                    BindData();
                    GetSubInfo(ChinaSoft.Common.Input.SetInt(Request.Params["SubInvoiceId"]));
                }
                if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
                {
                    BindEditInfo(ChinaSoft.Common.Input.SetInt(Request.Params["id"]));
                }
            }
        }

        private void GetSubInfo(int SubId)
        {
            ChinaSoft.BLL.Common.Bind bllBind = new ChinaSoft.BLL.Common.Bind();
            DataSet ds = bllBind.GetDataSet("InvoiceMoney,SumReceiveMoney", "V_FinaceSubInvoiceList", "SubInvoiceId=" + SubId);
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                hiddenSubInvoiceMoney.Value = ds.Tables[0].Rows[0]["InvoiceMoney"].ToString();
                lblMaxMoney.Text = (ChinaSoft.Common.Input.SetDecimal(hiddenSubInvoiceMoney.Value) - ChinaSoft.Common.Input.SetDecimal(ds.Tables[0].Rows[0]["SumReceiveMoney"])).ToString();
            }
        }

        private void BindEditInfo(int id)
        {
            ChinaSoft.Model.Model_tb_MidSoft_FinanceSubInvoicePayment model = bll.GetModel(id);
            txtReceiveDate.Value = Convert.ToDateTime(model.PayDate).ToShortDateString();
            txtReceiveMan.Text = model.PayMan;
            txtMoney.Value = model.PayMoney.ToString();
        }

        #region gridView
        public void BindData()
        {
            StringBuilder strWhere = new StringBuilder();
            if (Request.Params["SubInvoiceId"] != null && Request.Params["SubInvoiceId"].Trim() != "")
            {
                strWhere.AppendFormat(" and SubInvoiceId=" + Request.Params["SubInvoiceId"]);
            }
            if (txtCreateUserSearch.Text.Trim() != "")
            {
                strWhere.AppendFormat(" and PayMan like '%{0}%'", txtCreateUserSearch.Text.Trim());
            }
            if (txtBeginDate.Value != "" && txtEndDate.Value != "")
            {
                strWhere.AppendFormat(" and PayDate between '" + txtBeginDate.Value + "' and '" + txtEndDate.Value + "'");
            }
            else if (txtBeginDate.Value == "" && txtEndDate.Value != "")
            {
                strWhere.AppendFormat(" and PayDate<'" + txtEndDate.Value + "'");
            }
            else if (txtBeginDate.Value != "" && txtEndDate.Value == "")
            {
                strWhere.AppendFormat(" and PayDate>'" + txtBeginDate.Value + "'");
            }
            try
            {
                //lblCount.Text = (new ChinaSoft.BLL.Common.Pager()).RecordCount("V_ContractList", strWhere.ToString()).ToString();
                ChinaSoft.BLL.Common.Pager.BindGridView(gridView, AspNetPager1, "V_FinaceSubPayMentList", "SubInvoicePaymentId", "*", strWhere.ToString(), "CreateDate desc", "");
            }
            catch (Exception ex)
            {
                Response.Write("数据库错误：" + ex.Message);
                Response.End();
            }
        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }
        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView.PageIndex = e.NewPageIndex;
            BindData();
        }
        protected void gridView_OnRowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[0].Text = "<input type=\"checkbox\" id=\"cbSelectAll\" name=\"cbSelectAll\" onclick=\"javascript:checkJs(this.checked);\" />";
            }
        }
        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //LinkButton linkbtnDel = (LinkButton)e.Row.FindControl("LinkButton1");
                //linkbtnDel.Attributes.Add("onclick", "return confirm(\"你确认要删除吗\")");

                //object obj1 = DataBinder.Eval(e.Row.DataItem, "Levels");
                //if ((obj1 != null) && ((obj1.ToString() != "")))
                //{
                //    e.Row.Cells[1].Text = obj1.ToString();
                //}

            }
        }
        protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //#warning 代码生成警告：请检查确认真实主键的名称和类型是否正确
            int ID = (int)gridView.DataKeys[e.RowIndex].Value;
            if (ID.ToString().Trim().Length == 0)
                return;
            ///对数据做删除
            bll.Delete(ID);
            BindData();
            //gridView.OnBind();
        }
        private string GetSelIDlist()
        {
            string idlist = "";
            bool BxsChkd = false;
            for (int i = 0; i < gridView.Rows.Count; i++)
            {
                CheckBox ChkBxItem = (CheckBox)gridView.Rows[i].FindControl("checkboxname");
                if (ChkBxItem != null && ChkBxItem.Checked)
                {
                    BxsChkd = true;
                    //#warning 代码生成警告：请检查确认Cells的列索引是否正确
                    if (gridView.DataKeys[i].Value != null)
                    {
                        idlist += gridView.DataKeys[i].Value.ToString() + ",";
                    }
                }
            }
            if (BxsChkd)
            {
                idlist = idlist.Substring(0, idlist.LastIndexOf(","));
            }
            return idlist;
        }
        #endregion

        protected void btnSearch_Click(object sender, ImageClickEventArgs e)
        {
            BindData();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string strErr = "";

            if (!PageValidate.IsAllNumber(txtMoney.Value))
            {
                strErr += "收款金额格式错误！\\n";
            }

            if (this.txtReceiveMan.Text.Trim().Length == 0)
            {
                strErr += "收款人不能为空！\\n";
            }
            if (this.txtReceiveDate.Value.Trim().Length == 0)
            {
                strErr += "收款日期不能为空！\\n";
            }
            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            int SubInvoiceId = ChinaSoft.Common.Input.SetInt(Request.Params["SubInvoiceId"]);
            ChinaSoft.BLL.BLL_tb_MidSoft_FinanceSubInvoice bllFinanceMainInvoice = new ChinaSoft.BLL.BLL_tb_MidSoft_FinanceSubInvoice();

            ChinaSoft.Model.Model_tb_MidSoft_FinanceSubInvoice modelFinanceMainInvoice = bllFinanceMainInvoice.GetModel(SubInvoiceId);


            int ProjectInfoId = modelFinanceMainInvoice.ProjectInfoId;
            int ConstractId = modelFinanceMainInvoice.ContractId;
            decimal ReceiveMoney = decimal.Parse(this.txtMoney.Value);
            DateTime ReceiveDate = DateTime.Parse(this.txtReceiveDate.Value);
            string ReceiveMan = this.txtReceiveMan.Text;
            DateTime CreateDate = DateTime.Now;
            int CreateUserId = ChinaSoft.Common.Input.SetInt(Session["currentUserID"]);
            if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
            {
                ChinaSoft.Model.Model_tb_MidSoft_FinanceSubInvoicePayment model = bll.GetModel(ChinaSoft.Common.Input.SetInt(Request.Params["id"]));
                model.PayMan = ReceiveMan;
                model.PayMoney = ReceiveMoney;
                model.PayDate = ReceiveDate;

                bll.Update(model);
            }
            else
            {
                ChinaSoft.Model.Model_tb_MidSoft_FinanceSubInvoicePayment model = new ChinaSoft.Model.Model_tb_MidSoft_FinanceSubInvoicePayment();
                model.ProjectInfoId = ProjectInfoId;
                model.ConstractId = ConstractId;
                model.SubInvoiceId = SubInvoiceId;
                model.PayMoney = ReceiveMoney;
                model.PayDate = ReceiveDate;
                model.PayMan = ReceiveMan;
                model.CreateDate = CreateDate;
                model.CreateUserId = CreateUserId;

                ChinaSoft.BLL.BLL_tb_MidSoft_FinanceSubInvoicePayment bll = new ChinaSoft.BLL.BLL_tb_MidSoft_FinanceSubInvoicePayment();
                bll.Add(model);
            }

            Response.Redirect("/Finance/SubInvoicePayMent/Add.aspx?SubInvoiceId=" + SubInvoiceId);
            //Response.Write("<script>alert('成功！');top.window.parent.frames.item('workspace').location.reload();window.parent.$('#DivSysiframe').dialog(\"close\");</script>");
        }
    }
}